package com.kc.business.service.impl;

import com.kc.business.domain.BizBusinessGoods;
import com.kc.business.mapper.BizBusinessGoodsMapper;
import com.kc.business.service.IBizBusinessGoodsService;
import com.kc.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商家商品service实现类
 */
@Service
public class BizBusinessGoodsServiceImpl implements IBizBusinessGoodsService {
    @Autowired
    private BizBusinessGoodsMapper businessGoodsMapper;
    /**
     * 获取商品列表
     * @param bizBusinessGoods
     * @return
     */
    @Override
    public List<BizBusinessGoods> getList(BizBusinessGoods bizBusinessGoods) {
        List<BizBusinessGoods> bizBusinessGoodsList = businessGoodsMapper.getList(bizBusinessGoods);
        bizBusinessGoodsList.forEach(bizBusinessGoods1 -> {
            bizBusinessGoods1.setGoodsLables(StringUtils.split(bizBusinessGoods1.getGoodsLable(),","));
            if(StringUtils.isNotEmpty(bizBusinessGoods1.getGoodsImage())) {
                bizBusinessGoods1.setGoodsImageList(bizBusinessGoods1.getGoodsImage().split(","));
            }
        });
        return bizBusinessGoodsList;
    }

    /**
     * 获取商品详情
     * @param bizBusinessGoods
     * @return
     */
    @Override
    public BizBusinessGoods getListInfo(BizBusinessGoods bizBusinessGoods) {
        return businessGoodsMapper.selectByPrimaryKey(bizBusinessGoods.getId());
    }

    /**
     * 添加商品
     * @param bizBusinessGoods
     * @return
     */
    @Override
    public Integer add(BizBusinessGoods bizBusinessGoods) {
        if(bizBusinessGoods.getStock() != null && bizBusinessGoods.getStock() != 0){
            bizBusinessGoods.setIsSetStock(1);
        }
        bizBusinessGoods.setGoodsImage(StringUtils.join(bizBusinessGoods.getGoodsImageList(),","));
        return businessGoodsMapper.insert(bizBusinessGoods);
    }
    /**
     * 编辑商品
     * @param bizBusinessGoods
     * @return
     */
    @Override
    public Integer edit(BizBusinessGoods bizBusinessGoods) {
        if(bizBusinessGoods.getStock() != null && bizBusinessGoods.getStock() != 0){
            bizBusinessGoods.setIsSetStock(1);
        }
        bizBusinessGoods.setGoodsImage(StringUtils.join(bizBusinessGoods.getGoodsImageList(),","));
        return businessGoodsMapper.updateByPrimaryKey(bizBusinessGoods);
    }
    /**
     * 删除商品
     * @param bizBusinessGoods
     * @return
     */
    @Override
    public Integer remove(BizBusinessGoods bizBusinessGoods) {
        return businessGoodsMapper.updateByPrimaryKey(bizBusinessGoods);
    }

    @Override
    public Integer getGoodsNum(Integer businessId) {
        BizBusinessGoods bizBusinessGoods = new BizBusinessGoods();
        bizBusinessGoods.setBusinessId(businessId);
        return businessGoodsMapper.selectCount(bizBusinessGoods);
    }
}
